package com.jdojo.node;

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class CoordinateConversion extends Application {
    // An instance variable to store the reference of the circle
    private Circle marker;

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        TextField fName = new TextField();
        TextField lName = new TextField();
        TextField salary = new TextField();
        // The Circle node is unmanaged
        marker = new Circle(5);
        marker.setManaged(false);
        marker.setFill(Color.RED);
        marker.setMouseTransparent(true);
        HBox hb1 = new HBox();
        HBox hb2 = new HBox();
        HBox hb3 = new HBox();
        hb1.getChildren().addAll(new Label("First Name:"), fName);
        hb2.getChildren().addAll(new Label("Last Name:"), lName);
        hb3.getChildren().addAll(new Label("Salary:"), salary);
        VBox root = new VBox();
        root.getChildren().addAll(hb1, hb2, hb3, marker);
        Scene scene = new Scene(root);
        // Add a focus change listener to the scene
        scene.focusOwnerProperty().addListener(
                (prop, oldNode, newNode) -> placeMarker(newNode));
        stage.setScene(scene);
        stage.setTitle("Coordinate Space Transformation");
        stage.show();
    }

    public void placeMarker(Node newNode) {
        double nodeMinX = newNode.getLayoutBounds().getMinX();
        double nodeMinY = newNode.getLayoutBounds().getMinY();
        // 获取TextField节点在本地坐标系中的位置
        System.out.println("node:" + nodeMinX + "," + nodeMinY);

        // 获取TextField节点在场景中的位置
        Point2D nodeInScene = newNode.localToScene(nodeMinX, nodeMinY);
        System.out.println("nodeInScene:" + nodeInScene.getX() + "," + nodeInScene.getY());

        // 将TextField节点相对于场景的坐标转换为相对于另一个节点（marker）的本地坐标系中的坐标
        Point2D nodeInMarkerLocal = marker.sceneToLocal(nodeInScene);
        System.out.println("nodeInMarkerLocal:" + nodeInMarkerLocal.getX() + "," + nodeInMarkerLocal.getY());

        // 将TextField节点相对于另一个节点（marker）的本地坐标系中的坐标转换为相对于 marker 的父级容器坐标系中的坐标
        Point2D nodeInMarkerParent = marker.localToParent(nodeInMarkerLocal);
        System.out.println("nodeInMarkerParent:" + nodeInMarkerParent.getX() + "," + nodeInMarkerParent.getY());
        System.out.println("marker:" + marker.getLayoutBounds().getMinX() + "," + marker.getLayoutBounds().getMinY() + "," + marker.getLayoutBounds().getWidth() + "," + marker.getLayoutBounds().getHeight()
                + "," + marker.getRadius());
        // Position the circle approperiately
        marker.relocate(nodeInMarkerParent.getX() - marker.getLayoutBounds().getWidth() + marker.getRadius() / 2
                ,
                nodeInMarkerParent.getY() - marker.getLayoutBounds().getHeight() + marker.getRadius() / 2
        );
    }
}
